/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.hanfu.domain.TableSampleClass;
import top.continew.admin.hanfu.domain.query.TableSampleClassQuery;
import top.continew.admin.hanfu.service.impl.TableSampleClassServiceImpl;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-04-15
 */
@RestController
@RequestMapping("/table-sample-class")
@RequiredArgsConstructor
public class TableSampleClassController {

    private final TableSampleClassServiceImpl sampleClassService;

    @PostMapping
    public R<Void> addTableSampleClass(@RequestBody TableSampleClass tableSampleClass) {
        tableSampleClass.setUpdateTime(System.currentTimeMillis() + "");
        sampleClassService.save(tableSampleClass);
        return R.success();
    }

    @PutMapping("")
    public R<Void> updateTableSampleClass(@RequestBody TableSampleClass tableSampleClass) {
        tableSampleClass.setUpdateTime(System.currentTimeMillis() + "");
        sampleClassService.updateById(tableSampleClass);
        return R.success();
    }

    @DeleteMapping("/{id}")
    public R<Void> delTableSampleClass(@PathVariable Integer id) {
        sampleClassService.removeById(id);
        return R.success();
    }

    @PostMapping("/list")
    public R<Page<TableSampleClass>> getTableSampleClass(@RequestBody TableSampleClassQuery query) {
        Page<TableSampleClass> page = new Page<>(query.getPageNum(), query.getPageSize());

        TableSampleClass sampleClass = new TableSampleClass();
        sampleClass.setSampleClassName(query.getSampleClassName().isEmpty() ? null : query.getSampleClassName());

        Page<TableSampleClass> sampleClassPage = sampleClassService.getSampleClassPage(page, sampleClass);

        sampleClassPage.getRecords().forEach(item -> {
            item.setUpdateTime(DateUtil.date(Long.parseLong(item.getUpdateTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });

        return R.success(sampleClassPage);
    }

    @SaIgnore
    @GetMapping("/wx/list")
    public R<List<TableSampleClass>> getTableSampleClassWx() {

        List<TableSampleClass> sampleClassList = sampleClassService.lambdaQuery()
            .eq(TableSampleClass::getStatus, 1)
            .list();

        return R.success(sampleClassList);
    }

}
